﻿using UnityEngine;
using System.Collections;
using FutureCode.Game.Command;

public class UIEventProcessor : MonoBehaviour {

    public GameObject HostConnectingPanel;
    public GameObject HostSettingPanel;
    public UIInput HostIP;
    public UIInput HostPort;
    public UIButton ConnectButton;
    public UIToggle EnableStrategyCheckBox;
    public UIToggle ShowShadows;
    public UISlider AdjustIntervalSlider;
    public UISlider MaxMonsterCountSlider;
    public UILabel Ping;
    public UILabel LabelAdjustIntervalValue;
    public UILabel LabelMaxMonsterCountValue;
    public void OnEnableStrategyCheckBoxValueChanged()
    {
        GameSettings.Instance.EnableSyncStrategy = UIToggle.current.value;
        WhetherEnableSyncStrategyCommand cmd = new WhetherEnableSyncStrategyCommand();
        cmd.EnableSyncStrategy = GameSettings.Instance.EnableSyncStrategy;
        NetManager.Instance.Send(cmd);
    }
    public void OnShowShadowsCheckBoxValueChanged()
    {
        bool show = UIToggle.current.value;
        WhetherShowShadowsCommand cmd = new WhetherShowShadowsCommand();
        cmd.Show = show;
        NetManager.Instance.Send(cmd);
    }
    public void OnMaxMonsterCountSliderValueChanged()
    {
        MonsterManager mm = MonsterManager.Instance;
        int count = (((int)(UISlider.current.value * 10))) * 5;
        //count = 1;

        var mmccc = new MaxMonsterCountChangedCommand();
        mmccc.MaxCount = count;
        NetManager.Instance.Send(mmccc);

        mm.MaxMonsterCount = count;
        if (mm.ExistsMonsterCount > count)
        {
            var damc = new DestoryAllMonsterCommand();
            NetManager.Instance.Send(damc);
            mm.DestoryAllMonster();
        }
        LabelMaxMonsterCountValue.text = count.ToString();
    }
    public void OnConnectButtonClick()
    {
        GameSettings.Instance.HostIP = HostIP.value;
        GameSettings.Instance.HostPort = HostPort.value;
        HostSettingPanel.SetActive(false);
        HostConnectingPanel.SetActive(true);
        NetManager.Instance.ConnectToServer();
    }

    void Awake()
    {
        //绑定UI事件
        EventDelegate.Add(EnableStrategyCheckBox.onChange, OnEnableStrategyCheckBoxValueChanged);
        EventDelegate.Add(MaxMonsterCountSlider.onChange, OnMaxMonsterCountSliderValueChanged);
        EventDelegate.Add(ShowShadows.onChange, OnShowShadowsCheckBoxValueChanged);
        EventDelegate.Add(ConnectButton.onClick, OnConnectButtonClick);
    }

    void Start()
    {
        HostIP.value = GameSettings.Instance.HostIP;
        HostPort.value = GameSettings.Instance.HostPort;
    }

    void Update()
    {
        if (NetManager.Instance.Connected)
        {
            if (HostConnectingPanel.activeSelf)
                HostConnectingPanel.SetActive(false);
            Ping.text = NetManager.Instance.Ping.ToString();
        }
        else
            Ping.text = "9999";
    }
}
